
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Login</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }

        .login-container {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
            padding: 40px;
            width: 100%;
            max-width: 400px;
            transition: all 0.3s ease;
        }

        .login-container:hover {
            box-shadow: 0 6px 25px rgba(0, 0, 0, 0.15);
        }

        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .login-header h1 {
            color: #333;
            font-size: 28px;
            font-weight: 600;
            margin-bottom: 10px;
        }

        .login-header p {
            color: #666;
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-size: 14px;
            font-weight: 500;
        }

        .form-control {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 14px;
            transition: border 0.3s ease;
        }

        .form-control:focus {
            outline: none;
            border-color: #4a90e2;
            box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.1);
        }

        .btn-login {
            width: 100%;
            padding: 12px;
            background-color: #4a90e2;
            color: white;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        .btn-login:hover {
            background-color: #3a7bc8;
        }

        .error-message {
            margin-top: 20px;
            padding: 12px;
            background-color: #ffebee;
            color: #d32f2f;
            border-radius: 6px;
            text-align: center;
            font-size: 14px;
            display: none;
        }

        .error-message.show {
            display: block;
        }

        .footer-links {
            margin-top: 20px;
            text-align: center;
            font-size: 13px;
        }

        .footer-links a {
            color: #4a90e2;
            text-decoration: none;
        }

        .footer-links a:hover {
            text-decoration: underline;
        }
    </style>
    <script th:src="@{https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js}"></script>
    <script th:src="@{https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/aes.min.js}"></script>
</head>
<body>
<div class="login-container">
    <div class="login-header">
        <h1>欢迎登录</h1>
        <p>请输入您的账号和密码</p>
    </div>

    <form th:id="loginForm" th:action="@{/login}" th:object="${user}" method="post">
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" th:field="*{username}" class="form-control" placeholder="请输入用户名" required>
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" class="form-control" placeholder="请输入密码" required>
        </div>

        <input type="hidden" id="encryptedPassword" th:field="*{password}" name="encryptedPassword"/>


        <button type="submit" class="btn-login">登 录</button>
    </form>


    <!-- 错误信息显示 -->
    <div th:classappend="${param.error} ? 'show' : ''" class="error-message">
        <i class="fas fa-exclamation-circle"></i> 用户名或密码错误！
    </div>

    <div class="footer-links">
        <a href="#">忘记密码?</a> | <a href="#">注册账号</a>
    </div>
</div>
</body>
<script>
    document.getElementById('loginForm').addEventListener('submit', function(e) {
        e.preventDefault();

        // 获取明文密码
        const password = document.getElementById('password').value;

        // 加密密钥（应与后端一致，可以是固定值或动态生成）
        const secretKey = "1234567890123456";

        // 使用AES加密
        const encryptedPassword = CryptoJS.AES.encrypt(
            CryptoJS.enc.Utf8.parse(password),
            CryptoJS.enc.Utf8.parse(secretKey),
            {
                mode: CryptoJS.mode.ECB, // 与后端一致
                padding: CryptoJS.pad.Pkcs7
            }
        ).toString();

        // 将加密后的密码放入隐藏字段
        document.getElementById('encryptedPassword').value = encryptedPassword;

        // 提交表单
        this.submit();
    });
</script>
</html>